package com.common.android.utils;

import java.io.File;
import java.io.FileFilter;
import java.util.Calendar;
import java.util.Locale;
import java.util.regex.Pattern;

import android.text.TextUtils;

/**
 * 删除存储器上的日志
 * <br>Created 2013-12-23 上午11:13:26
 * @version  1.1
 * @author   林胜杰		
 * @see 	 
 * Copyright(c) 2009-2014, TQ Digital Entertainment, All Rights Reserved
 *
 */
public final class EraseLogUtils {
    private static final int LOG_FILE_NAME_LEN = 14; // 2013-12-19.log共14个字符
    private static final int WEEK_DAY = 7; //一周有7天
    private static final int YEAR_EXPANTION = 1000; //年份扩大1000倍 
    private static final int MONTH_EXPANTION = 100; //月份扩大100倍
    private static final int LOG_NAME_LEN = 10; //日志名字长度
    private boolean mbRunning = false; // 是否运正在运行
	
    private static volatile EraseLogUtils instance;
	private String mstrSDCardCacheDir;
	private String mstrInternalCacheDir;

	/**
	 * Creates a new instance of EraseLogUtils.
	 * <br>Created 2014-8-26 下午4:25:42
	 * @param strSDCardCacheDir SD卡缓存目录
	 * @param strInternalCacheDir 网络缓存目录
	 */
	private EraseLogUtils(String strSDCardCacheDir, String strInternalCacheDir) {
        mstrSDCardCacheDir = new String(strSDCardCacheDir);
        mstrInternalCacheDir = new String(strInternalCacheDir);
    }
	
	/**
	 * 获取实例
	 * <br>Created 2013-12-23 上午11:13:26
	 * @param strSDCardCacheDir SD卡缓存目录
	 * @param strInternalCacheDir 网络缓存目录
	 * @return 返回EraseLog实例
	 * @author       林胜杰
	 */
	public static EraseLogUtils getInstance(String strSDCardCacheDir,
			String strInternalCacheDir) {
		if (instance == null) {
			synchronized (EraseLogUtils.class) {
				if (instance == null) {
					instance = new EraseLogUtils(strSDCardCacheDir,
							strInternalCacheDir);
				}
			}
		}
		return instance;
	}

	/**
	 * 是否在运行删除日志
	 * <br>Created 2013-12-23 上午11:13:26
	 * @author       林胜杰
	 */
	public void EraseLogIfNotRunning() {
		if (!mbRunning) {
			mbRunning = true;
			eraseLogOneMonthAgo();
			mbRunning = false;
		}
	}

	/**
	 * 删除一个月前的日志
	 * <br>Created 2013-12-23 上午11:13:26
	 * @author       林胜杰
	 */
	private void eraseLogOneMonthAgo() {
		final String LOG_NAME = "Log";
		StringBuilder sbDir = new StringBuilder();

		// 先删SD卡
		if (!TextUtils.isEmpty(mstrSDCardCacheDir)) {
			eraseLogs(sbDir.append(mstrSDCardCacheDir).append(LOG_NAME)
					.append(File.separator).toString());
		}

		// 清空
		sbDir.delete(0, sbDir.length());

		// 再删内部存储器
		if (!TextUtils.isEmpty(mstrInternalCacheDir)) {
			eraseLogs(sbDir.append(mstrInternalCacheDir).append(LOG_NAME)
					.append(File.separator).toString());
		}
	}

	/**
	 * 删除指定路径下的日志
	 * <br>Created 2013-12-23 上午11:13:26
	 * @param strDir 指定路径
     * @author       林胜杰
	 */
	private void eraseLogs(String strDir) {
		File fDir = new File(strDir);

		// 计算当前日期减一周的整数值：20131219 --> 20131212 e.g.
		Calendar calendarCur = Calendar.getInstance();
		calendarCur.add(Calendar.DAY_OF_MONTH, - WEEK_DAY);
		final int iCriticalDate = calendarCur.get(Calendar.YEAR) * YEAR_EXPANTION
				+ (calendarCur.get(Calendar.MONTH) + 1) * MONTH_EXPANTION
				+ calendarCur.get(Calendar.DAY_OF_MONTH);

		// 取得所有一个月前的日志
		File[] dirs = fDir.listFiles(new FileFilter() {
			@Override
			public boolean accept(File pathname) {
				String strLogName = pathname.getName().toLowerCase(
						Locale.getDefault());
				if (strLogName.length() == LOG_FILE_NAME_LEN
						&& strLogName.endsWith(".log")) {
					Pattern pattern = Pattern
							.compile("\\d{4}-\\d{2}-\\d{2}.log");
					if (pattern.matcher(strLogName).matches()) {
						strLogName = strLogName.substring(0, LOG_NAME_LEN).replace("-",
								"");
						try {
							int iDateOfLog = Integer.parseInt(strLogName);
							if (iDateOfLog <= iCriticalDate) {
								return true;
							}
						} catch (NumberFormatException e) {
							return false;
						}
					}
					return false;
				}

				return false;
			}
		});

		if (null != dirs) {
			for (File file : dirs) {
				file.delete();
			}
		}
	}

}
